# -*- coding = utf-8 -*-
# name   : 分批删除数据
# Author : admin
# Email  : 
# File   : batch_delete_data.py
# Ver    : 1.0
# Time   : 2025-07-16 14:34
import time
from datetime import datetime, timedelta

# 分批删除参数
BATCH_SIZE = 5000  # 每批删除量
SLEEP_INTERVAL = 0.01  # 批次间隔(秒)
TABLE_NAME = "test_large_data4"


# 分批次 批量删除数据
def batch_delete(db_mongo, table_name=TABLE_NAME, days=30, date_lie: str = 'create_time', one_day=False):
    days_to_delete = (datetime.now() - timedelta(days=days)).strftime("%Y-%m-%d")

    # 删除条件（确保字段有索引！）
    delete_condition = {date_lie: {"$lt": days_to_delete}}
    if one_day:
        # 正则匹配日期，仅仅匹配一天
        days_to_delete_compact = days_to_delete.replace("-", "")
        condition = f"^(?:{days_to_delete}|{days_to_delete_compact})"
        delete_condition = {date_lie: {"$regex": condition}}

    print(f"// {table_name} mongo sql:")
    print(f"db.{table_name}.find({delete_condition}).count()")
    total_deleted = 0
    duration_all = 0
    while True:
        # 获取一批待删除文档的 _id
        cursor = db_mongo[table_name].find(
            filter=delete_condition,
            projection={"_id": 1},
            limit=BATCH_SIZE
        )
        ids_to_delete = [doc["_id"] for doc in cursor]

        if not ids_to_delete:
            break  # 无数据时退出

        start_time1 = time.time()
        # 执行批量删除
        result = db_mongo[table_name].delete_many({"_id": {"$in": ids_to_delete}})
        deleted_count = result.deleted_count
        total_deleted += deleted_count
        duration = time.time() - start_time1
        duration_all += duration
        print(f"已删除批次: {deleted_count} 条, 总计: {total_deleted} 条, 耗时: {duration:.2f} 秒")

        # 控制删除节奏
        time.sleep(SLEEP_INTERVAL)

    # print(f"删除完成! 共删除 {total_deleted} 条文档, 耗时: {duration_all:.2f} 秒.")
    return total_deleted, duration_all


if __name__ == "__main__":
    from utils.operate_mongodb import OperateMongodb
    from config import MONGODB_CONF as MONGODB_CONF

    mongo_object = OperateMongodb(MONGODB_CONF)
    _, db_mongo = mongo_object.conn_mongodb()

    total_deleted, duration_all = batch_delete(db_mongo, "test_large_data1", 30, "created_at", False)
    print(f"共删除 {total_deleted} 条文档, 耗时: {duration_all:.2f} 秒.")
